#include <stdlib.h>
#include <stdio.h>

const int ARR_LEN = 26;
const int arr[ARR_LEN] = {1,5,9,13,15,18,20,24,29,30,33,42,53,54,65,66,73,80,92,100,101,103,105,108,110,113};

int mybsearch (const int n)
{
    //printf ("%d\n", __LINE__);
    int left = 0;
    int right = ARR_LEN;
    int tmp = ( left + right ) / 2;
    while ( n != arr[tmp-1] && tmp > 0 && tmp <= ARR_LEN + 1 )
    {
        if ( n > arr[tmp-1] )
        {
    //printf ("%d\n", __LINE__);
            left = tmp + 1;
        }
        else
        {
    //printf ("%d\n", __LINE__);
            right = tmp;
        }
        tmp = ( left + right ) / 2;
    }
    return tmp-1;
}

int mybsearch2 (const int key, const int * base, const int num )
{
    int left = 0, right = num;
    int mid = -1;
    while ( left < right )
    {
        mid = (left + right) / 2;
        if ( key < * (base + mid) )
        {
            right = mid;
        }
        else if ( key > *(base+mid) )
        {
            left = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return mid;
}



int main ()
{
    printf ("Index of 92 is %u.\n", mybsearch(92));
    printf ("Index of 110 is %u.\n", mybsearch2(110, arr, sizeof(arr)/sizeof(int)));
    int i;
    for ( i = 0; i < ARR_LEN; i++ )
    {
        printf ("Index of %d is %u.\n", arr[i], mybsearch(arr[i]));
    }
    for ( i = 0; i < ARR_LEN; i++ )
    {
        printf ("Index of %d is %u.\n", arr[i], mybsearch2(arr[i], arr, ARR_LEN));
    }
    return 0;
}
